This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
library(data.table)
library(tidyr)
library(maps)
library(haven)
library(ggplot2)
library(dplyr)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
library(readxl)
hardship_complete <- read_excel("/Users/cristinacandido/Documents/Github/risk_wvs/code/Hardship_complete_2024.xlsx")
hardship_complete
NA
NA
hardship_complete$homiciderate=log(hardship_complete$homiciderate)
hardship_complete$gdp=log(hardship_complete$gdp)
hardship_complete$infantmortality=log(hardship_complete$infantmortality)
hardship_complete$lifeexpectancy=log(hardship_complete$lifeexpectancy)
hardship_complete
# Reverse Codierung
hardship_complete$homiciderate=scale(hardship_complete$homiciderate)
hardship_complete$gdp=scale(-hardship_complete$gdp)
hardship_complete$infantmortality=scale(hardship_complete$infantmortality)
hardship_complete$lifeexpectancy=scale(-hardship_complete$lifeexpectancy)
hardship_complete$gini=scale(hardship_complete$gini)
hardship_complete$femalemale_primedu=scale(-hardship_complete$femalemale_primedu)
hardship_complete
hardship_complete$hardship_index=(hardship_complete$homiciderate+hardship_complete$gdp+hardship_complete$gini+hardship_complete$lifeexpectancy+hardship_complete$infantmortality+hardship_complete$femalemale_primedu)/6
hardship_complete
# Laura added this code
library(dplyr)
# Impute missing values in the hardship indicators using the median
hardship_complete <- hardship_complete %>%
mutate(across(c("homiciderate", "gdp", "infantmortality", "lifeexpectancy", "gini", "femalemale_primedu"),
~ifelse(is.na(.), median(., na.rm = TRUE), .)))
hardship_complete
# Use the mutate function to change the country name
hardship_complete <- hardship_complete %>%
mutate(country = ifelse(label == "Serbia and Montenegro", "Serbia", label))
hardship_complete
# Data of Wave 5
WV5_data <- readRDS("/Users/cristinacandido/Documents/Github/risk_wvs/data/WVS/F00007944-WV5_Data_R_v20180912.rds")
# Convert WV5_data-object in data.frame
WV5_data_df <- as.data.frame(WV5_data)
# show first five columns
WV5_data_df
#rename the variables
WV5_data <- WV5_data_df %>%
rename(gender = V235, age = V237, country_code = V2, wave = V1, risktaking = V86, children = V56, married = V55, employed = V241, education = V238)
WV5_data
colnames(WV5_data)
[1] "wave" "V1A" "V1B" "country_code" "V2A" "V3" "V4" "V4_CO" "V5"
[10] "V5_CO" "V6" "V6_CO" "V7" "V7_CO" "V8" "V8_CO" "V9" "V9_CO"
[19] "V10" "V11" "V12" "V13" "V14" "V15" "V16" "V17" "V18"
[28] "V19" "V20" "V21" "V22" "V23" "V24" "V25" "V26" "V27"
[37] "V28" "V29" "V30" "V31" "V32" "V33" "V34" "V35" "V36"
[46] "V37" "V38" "V39" "V40" "V41" "V42" "V43" "V43_01" "V43_02"
[55] "V43_03" "V43_04" "V43_05" "V43_06" "V43_07" "V43_08" "V43_09" "V43_10" "V43_11"
[64] "V43_12" "V43_13" "V43_14" "V43_15" "V43_16" "V43_17" "V43_18" "V43_19" "V43_20"
[73] "V43_21" "V43_22" "V43_23" "V43_24" "V43_25" "V43_26" "V43_27" "V43_28" "V43_29"
[82] "V43_30" "V44" "V45" "V46" "V47" "V48" "V49" "V50" "V51"
[91] "V52" "V53" "V54" "married" "children" "V57" "V58" "V59" "V60"
[100] "V61" "V62" "V63" "V64" "V65" "V66" "V67" "V68" "V69"
[109] "V69_HK" "V70" "V70_HK" "V71" "V72" "V73" "V73_HK" "V74" "V74_HK"
[118] "V75" "V76" "V77" "V78" "V79" "V80" "V81" "V82" "V83"
[127] "V84" "V85" "risktaking" "V87" "V88" "V89" "V90" "V91" "V92"
[136] "V93" "V94" "V95" "V96" "V97" "V98" "V99" "V100" "V101"
[145] "V102" "V103" "V104" "V105" "V106" "V107" "V108" "V109" "V110"
[154] "V111" "V112" "V113" "V114" "V115" "V116" "V117" "V118" "V119"
[163] "V120" "V121" "V122" "V123" "V124" "V125" "V126" "V127" "V128"
[172] "V129" "V130" "V130_CA_1" "V130_IQ_1" "V130_IQ_2" "V130_IQ_3" "V130_IQ_4" "V130_NZ_1" "V130_NZ_2"
[181] "V131" "V132" "V133" "V134" "V135" "V136" "V137" "V138" "V139"
[190] "V140" "V141" "V142" "V143" "V144" "V145" "V146_00" "V146_01" "V146_02"
[199] "V146_03" "V146_04" "V146_05" "V146_06" "V146_07" "V146_08" "V146_09" "V146_10" "V146_11"
[208] "V146_12" "V146_13" "V146_14" "V146_15" "V146_16" "V146_17" "V146_18" "V146_19" "V146_20"
[217] "V146_21" "V146_22" "V147" "V148" "V149" "V150" "V151" "V151_IQ_A" "V151_IQ_B"
[226] "V152" "V153" "V154" "V155" "V156" "V157" "V158" "V159" "V160"
[235] "V161" "V162" "V163" "V164" "V165" "V166" "V167" "V168" "V169"
[244] "V170" "V171" "V172" "V173" "V174" "V175" "V176" "V177" "V178"
[253] "V179" "V180" "V181" "V182" "V183" "V184" "V185" "V186" "V187"
[262] "V188" "V189" "V190" "V191" "V192" "V193" "V194" "V195" "V196"
[271] "V197" "V198" "V199" "V200" "V201" "V202" "V203" "V204" "V205"
[280] "V206" "V207" "V208" "V209" "V210" "V211" "V212" "V213A" "V213B"
[289] "V213C" "V213D" "V213E" "V213F" "V213G" "V213H" "V213K" "V213L" "V213M"
[298] "V213N" "V214" "V215" "V216" "V217" "V218" "V219" "V220" "V221"
[307] "V222" "V223" "V224" "V225" "V226" "V227" "V228" "V229" "V230"
[316] "V231" "V232" "V233" "V233A" "V234" "gender" "V236" "age" "education"
[325] "V238CS" "V239" "V240" "employed" "V242" "V242A_CO" "V243" "V244" "V245"
[334] "V246" "V247" "V248" "V249" "V250" "V251" "V252" "V252B" "V253"
[343] "V253CS" "V254" "V255" "V255CS" "V256" "V257" "V257B" "V257C" "V258"
[352] "V259" "V259A" "V260" "V261" "V262" "V263" "V264" "V265" "S024"
[361] "S025" "Y001" "Y002" "Y003" "SACSECVAL" "SECVALWGT" "RESEMAVAL" "WEIGHTB" "I_AUTHORITY"
[370] "I_NATIONALISM" "I_DEVOUT" "DEFIANCE" "WEIGHT1A" "I_RELIGIMP" "I_RELIGBEL" "I_RELIGPRAC" "DISBELIEF" "WEIGHT2A"
[379] "I_NORM1" "I_NORM2" "I_NORM3" "RELATIVISM" "WEIGHT3A" "I_TRUSTARMY" "I_TRUSTPOLICE" "I_TRUSTCOURTS" "SCEPTICISM"
[388] "WEIGHT4A" "I_INDEP" "I_IMAGIN" "I_NONOBED" "AUTONOMY" "WEIGHT1B" "I_WOMJOB" "I_WOMPOL" "I_WOMEDU"
[397] "EQUALITY" "WEIGHT2B" "I_HOMOLIB" "I_ABORTLIB" "I_DIVORLIB" "CHOICE" "WEIGHT3B" "I_VOICE1" "I_VOICE2"
[406] "I_VOI2_00" "VOICE" "WEIGHT4B" "S001" "S007" "S018" "S019" "S021" "COW"
#select only the variables of interest
WV5_data <- WV5_data %>%
dplyr::select(gender, age, country_code, wave, risktaking, children, married, employed, education)
WV5_data
#Read Dataset (Wave 6)
WV6_data <- load("/Users/cristinacandido/Documents/Github/risk_wvs/data/WVS/WV6_Data_R_v20201117.rdata")
WV6_data <- WV6_Data_R_v20201117
print(WV6_data)
#Read Dataset (Wave 6)
WV6_data <- load("/Users/cristinacandido/Documents/Github/risk_wvs/data/WVS/WV6_Data_R_v20201117.rdata")
WV6_data <- WV6_Data_R_v20201117
print(WV6_data)